import BaseAnimationPainting from "../../standard/entities/Painting/BaseAnimationPainting";
import Movement2D from "../../standard/entities/Movement/Movement2D";
import Point2D from "../../standard/entities/Point/Point2D";
import TrackEffect from "../../standard/entities/Effect/TrackEffect";
import DrawnStep from "../../standard/entities/DrawnStep/DrawnStep";
import GravitationAcceleration2D from "../Movement/GravitationAcceleration2D";
import eUtils from "../../standard/utils/every-utils";

export default class AnimatePointPainting extends BaseAnimationPainting {

    constructor({centre = new Point2D(), color = '#fff'}) {
        super();
        this.movement = new Movement2D({speed: 5, angle: Math.floor(Math.random() * 360),maxSpeed:9999});
        this.centre = centre;
        this.oldCentre = new Point2D(centre.x, centre.y);
        this.color = color;
        this.r = 1;
    }


    onPainterLoaded(painter) {
        super.onPainterLoaded(painter);
        this.movement.applyAcceleration('gravitation', new GravitationAcceleration2D(
            new Point2D(400, 400), 1
        ));
        this.activeDrawnStep((painter, data) => {
            painter.drawInSession(() => {
                // painter.beginPath();
                // painter.strokeStyle = data.color;
                // painter.fillStyle = data.color;
                // painter.arc(data.centre.x, data.centre.y, data.r, 0, 2 * Math.PI);
                // painter.fill();
                // painter.stroke();
                painter.lineCap = painter.lineJoin = "round";
                painter.lineWidth = data.r;
                painter.strokeStyle = data.color;
                painter.fillStyle = data.color;
                painter.moveTo(data.oldCentre.x, data.oldCentre.y);
                painter.lineTo(data.centre.x, data.centre.y);
                painter.stroke();
            });
        });
        // painter.lineCap = painter.lineJoin = "round";
        // painter.lineWidth = this.r * 2;
        // painter.strokeStyle = data.color;
        // painter.fillStyle = data.color;
        // painter.moveTo(data.oldCentre.x, data.oldCentre.y);
        // painter.lineTo(data.centre.x, data.centre.y);
        // painter.stroke();

        this.setEffects([new TrackEffect(this.drawnStep, 1, 1, 0.01)]);
    }

    nextFrame(painter) {
        super.nextFrame(painter);
        this.oldCentre.x = this.centre.x;
        this.oldCentre.y = this.centre.y;
        this.movement.move(this.centre);
    }

    exportDrawnData() {
        return {
            centre: eUtils.cloneObject(this.centre),
            oldCentre: eUtils.cloneObject(this.oldCentre),
            color: this.color,
            r: this.r
        }
    }
}